package com.corner.ui.activity;

import android.view.View;

import com.corner.R;
import com.corner.model.db.Session;
import com.corner.presenter.imp.NotifyPresenter;
import com.corner.ui.adapter.NotifyAdapter;
import com.corner.ui.base.BaseActivity;
import com.corner.ui.view.INotifyView;
import com.corner.utils.AppManager;
import com.corner.utils.view.FindView;
import com.corner.utils.view.MainActionBar;
import com.corner.utils.view.widget.XListView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2015/5/19.  通知
 */

public class NotifyActivity extends BaseActivity<NotifyPresenter> implements INotifyView, View.OnClickListener {
    private MainActionBar actionBar;
    @FindView(R.id.lv_notify)
    private XListView lvNotify;

    private NotifyAdapter adapter;

    @Override
    protected NotifyPresenter createP() {
        return new NotifyPresenter();
    }

    @Override
    protected void setOnClick() {
        actionBar.btnLeft.setOnClickListener(this);
        lvNotify.setAutoLoadEnable(false);
        lvNotify.setPullLoadEnable(false);
        lvNotify.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                presenter.refresh();
            }

            @Override
            public void onLoadMore() {
                presenter.loadMore();
            }
        });
    }

    @Override
    protected void initData() {
        adapter = new NotifyAdapter(new ArrayList<Session>(), NotifyActivity.this);
        lvNotify.setAdapter(adapter);
        presenter.refresh();
    }


    @Override
    public void setContentView() {
        setContentView(R.layout.activity_notify);
    }

    @Override
    public void initView() {
        super.initView();
        actionBar = new MainActionBar(this);
        actionBar.initActionBarButton();
        actionBar.btnLeft.setVisibility(View.VISIBLE);
        actionBar.btnLeft.setText(getString(R.string.activity_notify_action_bar_left_str_message));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_actionbar_left:
                AppManager.getInstance().finishActivity(this);
        }
    }


    @Override
    public void showRefreshSession(List<Session> sessions) {
        if (adapter == null) {
            adapter = new NotifyAdapter(sessions, NotifyActivity.this);
            lvNotify.setAdapter(adapter);
        } else {
            adapter.changeList(sessions);
        }
    }

    @Override
    public void showLoadMoreSession(List<Session> sessions) {
        adapter.changeList(sessions);
    }

    @Override
    public void refreshComplete() {
        lvNotify.stopRefresh();
    }

    @Override
    public void loadMoreComplete() {
        lvNotify.stopLoadMore();
    }

    @Override
    public void setLoadMoreEnable(boolean enable) {
        lvNotify.setPullLoadEnable(enable);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adapter != null) {
            adapter.onDestroy();
        }
    }
}
